<template>
  <div class="app_content">
      <el-form :inline="true" ref="queryForm" :model="queryParams" class="demo-form-inline" :size="size" v-if="showSearch">
        <el-form-item label="登录地址" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="登录状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="querySubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-search'"/>
          搜索</el-button>
        <el-button   @click="resetSubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-redo'"/>
          重置</el-button>
      </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            :size="size"
            :disabled="multiple"
            @click="handleDelete"
            v-hasRole="['system:logininfor:remove']"
          >
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-delete-five'"/>
          删除</el-button>
        </el-col>
        <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :size="size"
          @click="handleClean"
          v-hasRole="['system:logininfor:remove']"
        >
        <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-clear'"/>
        清空</el-button>
      </el-col>
        <!-- <el-col :span="1.5">
          <Export  :dc="true"  :dr="true" :exlData="tableData" :tHead="staticData.ExportHead" :tName="'导出的标题'" ></Export>
        </el-col>
  -->
        <!-- <RightToolbar :showSearch="showSearch" @queryTable="getList" :columns="columns"/> -->
        <RightToolbar :showSearch="showSearch" @queryTable='getList' @showSearchClick='showSearchClick' :columns="columns"/>
      </el-row>
      <el-table :border="$border" v-loading="loading" :data="tableData" :height="maxHeight"  style="width: 100%" @selection-change="handleSelectionChange"  :size="size"
      >
      <el-table-column type="selection" width="55" />
      <el-table-column label="编号" prop="id" show-overflow-tooltip key="id" v-if="columns[0].visible">
        <template #default="scope">{{scope.$index+1}}</template>
        </el-table-column>
      <el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true"  key="userName" v-if="columns[1].visible"/>
      <el-table-column label="登录地址" align="center" prop="ipaddr" width="130" :show-overflow-tooltip="true" key="ipaddr" v-if="columns[2].visible"/>
      <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true"  key="loginLocation" v-if="columns[3].visible"/>
      <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true"  key="browser" v-if="columns[4].visible"/>
      <el-table-column label="操作系统" align="center" prop="os" show-overflow-tooltip key="os" v-if="columns[5].visible"/>
      <el-table-column label="登录状态"  :formatter="statusFormat" show-overflow-tooltip  align="center" prop="status" key="status" v-if="columns[6].visible">
      </el-table-column>
      <el-table-column label="操作信息" align="center" show-overflow-tooltip prop="msg"  key="msg" v-if="columns[7].visible"/>
      <el-table-column label="登录时间" prop="loginTime" show-overflow-tooltip key="loginTime" v-if="columns[8].visible"/>
      
  </el-table>
  <div style="margin-top:10px;position: absolute;right: 0;margin-right:20px">
      <el-pagination
        v-model:currentPage="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
  </div>
  
  </div>
  
</template>

<script  >
import { list, delLogininfor, cleanLogininfor, unlockLogininfor } from "@/api/monitor/logininfor";
import { computed, ref ,reactive,onMounted,getCurrentInstance,toRefs} from 'vue'
import { useStore } from 'vuex';
import { useI18n } from 'vue-i18n';
export default defineComponent({
setup(){
  const { proxy } = getCurrentInstance();//关键代码
  const queryForm=ref(null)
  const { t } = useI18n();
  const store = useStore();
  const staticData=reactive({
    formData:{
    },
    maxHeight:store.getters['setting/mode']=='horizontal'?document.body.clientHeight - 265:store.getters['setting/isFullScreen']!=false ?document.body.clientHeight - 360:document.body.clientHeight - 320,
    ids:[],
    userName:[],
    single:true,
    multiple:true,
    showSearch:true,
    background:false,
    disabled :false,
    search:'',
    tableData:[],
    // 日期范围
    dateRange: [],
    // 菜单状态数据字典
    statusOptions: [],
    // 查询参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      ipaddr: undefined,
      userName: undefined,
      status: undefined
    },
    total:0,
    loading:true, 
    ExportHead:{
      id: "编号",
    },
    // 列信息
    columns: [
      { key: 0, label: `编号`, visible: true },
      { key: 1, label: `用户名称`, visible: true },
      { key: 2, label: `登录地址`, visible: true },
      { key: 3, label: `登录地点`, visible: true },
      { key: 4, label: `浏览器`, visible: true },
      { key: 5, label: `操作系统`, visible: true },
      { key: 6, label: `登录状态`, visible: true },
      { key: 7, label: `操作信息`, visible: true },
      { key: 8, label: `登录时间`, visible: true },
      
    ],
  })
  const size = computed(() => {
        return store.getters['setting/size'];
  });

  const small =computed(() => {
    return size.value=='small'?true:false
  });

    onMounted(() => {
      proxy.$getDicts("sys_common_status").then(response => {
        staticData.statusOptions = response.data;
      });
      proxy.getList()
      })

  const methods = {
     getList(){
        staticData.loading = true;
        list(staticData.queryParams).then(
          response => {
            staticData.tableData = response.rows;
            staticData.total = response.total;
            staticData.loading = false;
          }
      );
     },
    // 系统内置字典翻译
    statusFormat(row, column) {
      const data=proxy.$selectDictLabel(staticData.statusOptions, row.status)
      let stateClass
      // if(data=='正常'){
      //   stateClass='status_success'
      // }else{
      //   stateClass='status_error'
      // }
      
      return data;
    },

     /**
      * 显示或隐藏搜索条件
      **/
     showSearchClick(data){
       staticData.showSearch=data
     },
    
    // 多选框选中数据
    handleSelectionChange(selection) {
      staticData.ids = selection.map(item => item.infoId);
      staticData.userName = selection.map(item => item.userName);
      staticData.single = selection.length != 1;
      staticData.multiple = !selection.length;
    },
    /** 清空按钮操作 */
    handleClean() {
      ElMessageBox.confirm('是否确认清空所有登录日志数据项？', t('confirm.title'), {
          confirmButtonText: t('btn.confirm'),
          cancelButtonText: t('btn.cancel'),
          type: "warning"
        })
      .then(function() {
        return cleanLogininfor();
      }).then(() => {
        proxy.getList();
        proxy.Success("清空成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId  || staticData.ids;
      const userName = row.userName || staticData.userName;
      
      ElMessageBox.confirm('是否确认删除日志用户为"' + userName + '"的数据项？', t('confirm.title'), {
          confirmButtonText: t('btn.confirm'),
          cancelButtonText: t('btn.cancel'),
          type: "warning"
        })
      .then(function() {
        return delLogininfor(infoIds);
      }).then(() => {
        proxy.getList();
        proxy.Success("删除成功");
      }).catch(() => {});
    },
    resetSubmit(){
      staticData.dateRange=[]
      queryForm.value.resetFields();
      proxy.getList()
    },
    querySubmit(){
      staticData.queryParams.pageNum = 1;
      proxy.getList()
    },
    handleSizeChange (val) {
      console.log(`${val} items per page`)
      staticData.queryParams.pageSize =val;
      proxy.getList()
    },
    handleCurrentChange(val){
      console.log(`current page: ${val}`)
      staticData.queryParams.pageNum =val;
      proxy.getList()
    }
   }
    return {
      size,
      small,
      queryForm,
      ...methods,
      ...toRefs(staticData)
    }
}


})



</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
margin-top: 10px;
}
.demo-pagination-block .demonstration {
margin-bottom: 16px;
}
</style>
<style scoped>
.app_content{
  
  margin-top: 30px;
  margin-left: 10px;
}
.mb8{
  margin-bottom: 10px;
}
</style>